package com.yinhe.module.ls.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhBaseinfoFamily;
import com.yinhe.system.service.IYhBaseinfoFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 亲属信息Controller
 * 
 * @author yinhe
 * @date 2022-07-13
 */
@RestController
@RequestMapping("/ls/family")
public class LsBaseinfoFamilyController extends BaseController
{
    @Autowired
    private IYhBaseinfoFamilyService yhBaseinfoFamilyService;

    /**
     * 查询亲属信息列表
     */
    @PreAuthorize("@ss.hasPermi('module:ls:family:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhBaseinfoFamily yhBaseinfoFamily)
    {
        startPage();
        List<YhBaseinfoFamily> list = yhBaseinfoFamilyService.selectYhBaseinfoFamilyList(yhBaseinfoFamily);
        return getDataTable(list);
    }

    /**
     * 导出亲属信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:family:export')")
    @Log(title = "亲属信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhBaseinfoFamily yhBaseinfoFamily)
    {
        List<YhBaseinfoFamily> list = yhBaseinfoFamilyService.selectYhBaseinfoFamilyList(yhBaseinfoFamily);
        ExcelUtil<YhBaseinfoFamily> util = new ExcelUtil<YhBaseinfoFamily>(YhBaseinfoFamily.class);
        util.exportExcel(response, list, "亲属信息数据");
    }

    /**
     * 获取亲属信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:family:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhBaseinfoFamilyService.selectYhBaseinfoFamilyById(id));
    }


    /**
     * baseinfo跳转获取亲属信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:family:query')")
//    @GetMapping(value = "/{idNumber}")
//    public AjaxResult getToInfo(@PathVariable("idNumber") String idNumber)
//    {
//        return AjaxResult.success(yhBaseinfoFamilyService.selectYhBaseinfoFamilyByIdNumber(idNumber));
//    }


    /**
     * 新增亲属信息
     */
    @PreAuthorize("@ss.hasPermi('system:family:add')")
    @Log(title = "亲属信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhBaseinfoFamily yhBaseinfoFamily)
    {
        return toAjax(yhBaseinfoFamilyService.insertYhBaseinfoFamily(yhBaseinfoFamily));
    }



    /**
     * 修改亲属信息
     */
    @PreAuthorize("@ss.hasPermi('system:family:edit')")
    @Log(title = "亲属信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhBaseinfoFamily yhBaseinfoFamily)
    {
        return toAjax(yhBaseinfoFamilyService.updateYhBaseinfoFamily(yhBaseinfoFamily));
    }

    /**
     * 删除亲属信息
     */
    @PreAuthorize("@ss.hasPermi('system:family:remove')")
    @Log(title = "亲属信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhBaseinfoFamilyService.deleteYhBaseinfoFamilyByIds(ids));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        System.out.println("=============come==============");
        ExcelUtil<YhBaseinfoFamily> util = new ExcelUtil<YhBaseinfoFamily>(YhBaseinfoFamily.class);
        util.importTemplateExcel(response, "用户数据");
    }

    @Log(title = "信息导入", businessType = BusinessType.IMPORT)
   @PreAuthorize("@ss.hasPermi('system:family:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        //System.out.println("===============double=================");
        ExcelUtil<YhBaseinfoFamily> util = new ExcelUtil<YhBaseinfoFamily>(YhBaseinfoFamily.class);
        List<YhBaseinfoFamily> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = yhBaseinfoFamilyService.importFamily(userList, updateSupport,operName);
        return AjaxResult.success(message);
    }

}
